package com.zzg.test2.web.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.zzg.test2.R
import com.zzg.test2.web.WebSetting
import com.zzg.test2.web.manage.WebViewPreloader


class Tab2WebFragment: Fragment() {
    var frameLayout: FrameLayout? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_web, container, false)
        frameLayout = view.findViewById(R.id.web_frameLayout)
//        frameLayout!!.removeAllViews()
//        // 获取预加载的 WebView
//        // 获取预加载的 WebView
//        val webView = WebViewPreloader.getPreloadedWebView(context)
//        frameLayout!!.addView(webView)
//        webView = view.findViewById(R.id.webView)


        // 获取预加载的 WebView
        var  webView = getWebView()

        // 从原有父视图中移除
        (webView.parent as? ViewGroup)?.removeView(webView)

        // 添加到当前布局
        webView.let { frameLayout!!.addView(it) }
        webView.loadUrl("https://www.leadfund.com.cn/html5/index/advicer")
        return view
    }

    private fun getWebView():WebView {
        return if (WebViewPreloader.getWebViewIsInitialized()){
            WebViewPreloader.getPreloadedWebView(requireContext())
        }else{
            WebView(requireContext()).apply {
                WebSetting.setWebSetting(this)
            }
        }
    }
}